<?php

namespace app\api\Logic\Index;


use app\api\Logic\Interact\InteractLogic;
use app\api\model\IndexSettingModel;
use app\api\model\OnlineVideoModel;
use app\api\model\UserModel;

class IndexLogic
{

    public function getBanner($param)
    {
        $where = [
            'status' => 1,
            'type' => 2,
        ];
        $banner = db('campus_mall_goods_slideshow')->where($where)->where('shop_id',$param['shop_id'])->order('weigh desc')->field('id,shop_id,title,image,url')->select();
        if(!$banner){//校区轮播图没有 取总部轮播图
            $banner = db('campus_mall_goods_slideshow')->where($where)->where('shop_id',0)->order('weigh desc')->field('id,shop_id,title,image,url')->select();
        }
        return $banner;
    }


    /**
     * 获取首页瓷片区数据
     *
     * @param [type] $param 前端传入数据 user_id shop_id
     * @return void
     */
    public function getModuleData($param)
    {
        $where = [
            'shop_id' => ['in',[0,$param['shop_id']]],
            'deletetime' => null
        ];
        // 取出置顶数据，总部的优先
        $top_list = IndexSettingModel::where($where)->where('is_top',1)->order('is_top desc,shop_id asc')->limit(5)->select()->toArray();
        $top_num = count($top_list);//置顶数量

        if($top_num < 5){//置顶数量不足五条，补充剩余的普通数据
            $need = 5 - $top_num;
            // 取出普通数据，校区的优先
            $need_list = IndexSettingModel::where($where)->where('is_top',0)->order('shop_id desc')->limit($need)->select()->toArray();
            array_push($top_list,...$need_list);
        }
        return $top_list;
    }

    /**
     * 获取首页推荐视频数据
     *
     * @param [type] $param 前端传入数据 user_id shop_id
     * @return void
     */
    public function getVideoData($param)
    {
        $user_id = $param['user_id'];
        $video_type = [1];//可报名人员
        // 是否在读学员
        $user = UserModel::get($user_id);
        if($user['student_type'] == 2){
            $video_type = [1,2];
        }
        $where = [
            'a.shop_id' => ['in',[0,$param['shop_id']]],
            'a.deletetime' => null
        ];
        $list = OnlineVideoModel::where($where)
        ->alias('a')
        ->with(['videoTypeInfo'])
        ->where(function($query) use ($video_type,$user_id){
            $query
            ->where('a.type','in',$video_type)
            ->whereOr(function($q) use($user_id){//指定人员可看
                $q->where('a.type','3')
                    ->where("FIND_IN_SET('{$user_id}',a.user_ids)");
            });
        })
        ->join('admin b','a.admin_id = b.id','left')
        ->limit(5)
        ->field('a.id,a.shop_id,a.type_id,a.title,a.image,a.content,a.price,a.study_number,b.nickname')
        ->select();
        return $list;

    }

    /**
     * 获取首页推荐互动讨论
     *
     * @param [type] $param 前端传入数据 user_id shop_id
     * @return void
     */
    public function getRecommendInteract($param)
    {
        $normal_where = ['a.is_top' => 0];
        $interactLogic = new InteractLogic();
        if($param['page'] == 1){//获取第一页时候，取3条置顶数据，7条热门数据
            $top_where = ['a.is_top' => 1];
            $top_data = $interactLogic->getInteractList($param['shop_id'],$param['user_id'],$param['page'],3,0,1,$top_where);
            $top_num = count($top_data);
            $normal_num = 10 - $top_num;//热门数据需要取几条
            $normal_data = $interactLogic->getInteractList($param['shop_id'],$param['user_id'],$param['page'],$normal_num,0,2,$normal_where);
            array_push($top_data,...$normal_data);
            $data = $top_data;
        }else{//不是第一页时候，全部取热门数据
            $data = $interactLogic->getInteractList($param['shop_id'],$param['user_id'],$param['page'],$param['page_size'],0,2,$normal_where);
        }
        return $data;
    }

}
